package com.zmj.controller;

import com.zmj.entity.MyCode;
import com.zmj.utils.GetWord;
import com.zmj.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class UserController {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;


    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/getCode")
    @ResponseBody
    public Message getCode(String phone){
        Message message=new Message();
        String code=GetWord.gt();
        String num=phone.concat(":num");
        redisTemplate.opsForValue().set(phone,code,60, TimeUnit.SECONDS);
        if (redisTemplate.hasKey(num)){
            redisTemplate.opsForValue().increment(num);
            if ((int)redisTemplate.opsForValue().get(num)<3) {

                message.setMessage("此用户的验证码为"+code);

            }else {
                message.setMessage("验正码请求过多");
            }
        }else {
            redisTemplate.opsForValue().set(num, 0,5, TimeUnit.MINUTES);

            message.setMessage("此用户的验证码为"+code);
        }


        return message;
    }


    @RequestMapping("/verify")
    @ResponseBody
    public Message verify(MyCode myCode){
        Message message=new Message();

        if (redisTemplate.hasKey(myCode.getPhone())){
                if (myCode.getCode().equals(redisTemplate.opsForValue().get(myCode.getPhone()))){
                    List<String> keys =new ArrayList<>();
                    keys.add(myCode.getPhone()+":lock");
                    keys.add(myCode.getPhone());
                    keys.add(myCode.getPhone()+":num");
                    keys.add(myCode.getPhone()+":lock:num");
                    redisTemplate.delete(keys);
                    message.setCode(1);
                }else {
                    message.setCode(0);
                    if (redisTemplate.hasKey(myCode.getPhone()+":lock")){
                        redisTemplate.opsForValue().increment(myCode.getPhone()+":lock");
                        if ((int)redisTemplate.opsForValue().get(myCode.getPhone()+":lock")<4){
                            message.setMessage("验证码错误"+redisTemplate.opsForValue().get(myCode.getPhone()+":lock")+"次");
                        }else {
                            if (redisTemplate.hasKey(myCode.getPhone()+":lock:num")){
                                message.setCode(-1);
                                message.setMessage("你以无法验证并锁定一分钟"+redisTemplate.getExpire(myCode.getPhone()+":lock:num"));
                            }else {
                                redisTemplate.opsForValue().set(myCode.getPhone()+":lock:num","lock",1,TimeUnit.MINUTES);
                                message.setCode(-1);
                                message.setMessage("你以无法验证并锁定一分钟");
                            }

                        }
                    }else {
                        redisTemplate.opsForValue().set(myCode.getPhone()+":lock",1,1,TimeUnit.MINUTES);
                        message.setMessage("验证码错误"+redisTemplate.opsForValue().get(myCode.getPhone()+":lock")+"次");
                    }

                }
        }else {
            message.setCode(0);
            message.setMessage("手机验证码时间过期");
        }
        return message;
    }
}
